package com.example.handle;
import com.example.entity.Result;
import com.example.enums.ResultEnum;
import com.example.exception.StudentException;
import com.example.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author John
 */
@ControllerAdvice
@Slf4j
public class ExceptionHandle {


    @ExceptionHandler(value = StudentException.class)
    @ResponseBody
    public Result handle(Exception e){
        if(e instanceof StudentException){
            StudentException studentException = (StudentException) e;
            return ResultUtils.error(studentException.getCode(),studentException.getMessage());
        }else {
            log.error("【系统异常】{}",e);
            return ResultUtils.error(ResultEnum.UNKONW_ERROR.getCode(),ResultEnum.UNKONW_ERROR.getMsg());
        }
    }
}
